package com.whmskj.xjlhsc.mobie.ProductionManagement.service.imp;

import com.whmskj.xjlhsc.mobie.ProductionManagement.mapper.OutputEnergyConsumptionMapper;
import com.whmskj.xjlhsc.mobie.ProductionManagement.pojo.dto.OutputEnergyConsumptionByShiftDTO;
import com.whmskj.xjlhsc.mobie.ProductionManagement.pojo.entity.*;
import com.whmskj.xjlhsc.mobie.ProductionManagement.pojo.vo.*;
import com.whmskj.xjlhsc.mobie.ProductionManagement.service.IOutputEnergyConsumptionService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;

/**
 * @author jiaruchun
 * @date 2025-08-30 15:40
 */
@Slf4j
@Service
public class OutputEnergyConsumptionServiceImpl implements IOutputEnergyConsumptionService {
    @Autowired
    private OutputEnergyConsumptionMapper outputEnergyConsumptionMapper;

    @Override
    public EnergyConsumptionOfFineSandProductionListByDayProcessVO getOutputEnergyConsumption(String process, LocalDate startDate) {
        EnergyConsumptionOfFineSandProductionListByDayProcessVO energyConsumptionOfFineSandProductionListByDayVO = new EnergyConsumptionOfFineSandProductionListByDayProcessVO();
        if (startDate == null) {
            throw new IllegalArgumentException("startDate cannot be null");
        }
        LocalDate endDate = startDate.minusDays(29); // 向前30天
        List<EnergyConsumptionOfFineSandProductionVO> energyConsumptionOfFineSandProduction = outputEnergyConsumptionMapper.getMonthlyFineSandStats(process,startDate.toString(), endDate.toString());
        log.info("energyConsumptionOfFineSandProduction:{}", energyConsumptionOfFineSandProduction);
        //当天产量
        Float production = energyConsumptionOfFineSandProduction.get(energyConsumptionOfFineSandProduction.size() - 1).getProduction();
        energyConsumptionOfFineSandProductionListByDayVO.setProductionAmount(production);
        //当天折合产量
        Float convertIntoProduction = energyConsumptionOfFineSandProduction.get(energyConsumptionOfFineSandProduction.size() - 1).getConvertIntoProduction();
        energyConsumptionOfFineSandProductionListByDayVO.setProductionAmountConverted(convertIntoProduction);
        //产量列表
        ArrayList<ProductionList> productionLists = new ArrayList<>();
        for (EnergyConsumptionOfFineSandProductionVO energyConsumptionOfFineSandProductionVO : energyConsumptionOfFineSandProduction) {
            productionLists.add(new ProductionList(energyConsumptionOfFineSandProductionVO.getDate(), energyConsumptionOfFineSandProductionVO.getConvertIntoProduction(), energyConsumptionOfFineSandProductionVO.getProduction(), energyConsumptionOfFineSandProductionVO.getMorningShiftProduction(), energyConsumptionOfFineSandProductionVO.getNightShiftProduction()));
        }
        energyConsumptionOfFineSandProductionListByDayVO.setProductionVOList(productionLists);
        //产量列表2
        ArrayList<ProductionList2> productionLists2 = new ArrayList<>();
        for (EnergyConsumptionOfFineSandProductionVO energyConsumptionOfFineSandProductionVO : energyConsumptionOfFineSandProduction) {
            productionLists2.add(new ProductionList2(energyConsumptionOfFineSandProductionVO.getDate(), energyConsumptionOfFineSandProductionVO.getProduction(), energyConsumptionOfFineSandProductionVO.getConvertIntoProduction(), energyConsumptionOfFineSandProductionVO.getEnergyConsumptionPerTonneOfYarn()));
        }
        energyConsumptionOfFineSandProductionListByDayVO.setProductionVOList2(productionLists2);
        //当天能耗
        Float energyConsumption = energyConsumptionOfFineSandProduction.get(energyConsumptionOfFineSandProduction.size() - 1).getEnergy();
        energyConsumptionOfFineSandProductionListByDayVO.setEnergyConsumption(energyConsumption);
        //当天吨纱耗电
        Float energyConsumptionPerTonneOfYarn = energyConsumptionOfFineSandProduction.get(energyConsumptionOfFineSandProduction.size() - 1).getEnergyConsumptionPerTonneOfYarn();
        energyConsumptionOfFineSandProductionListByDayVO.setEnergyConsumptionPerTonneOfYarn(energyConsumptionPerTonneOfYarn);
        //能耗列表
        ArrayList<EnergyConsumptionList> energyConsumptionLists = new ArrayList<>();
        for (EnergyConsumptionOfFineSandProductionVO energyConsumptionOfFineSandProductionVO : energyConsumptionOfFineSandProduction) {
            energyConsumptionLists.add(new EnergyConsumptionList(energyConsumptionOfFineSandProductionVO.getDate(), energyConsumptionOfFineSandProductionVO.getEnergy(), energyConsumptionOfFineSandProductionVO.getMorningShiftEnergyConsumption(), energyConsumptionOfFineSandProductionVO.getNightShiftEnergyConsumption(), energyConsumptionOfFineSandProductionVO.getEnergyConsumptionPerTonneOfYarn()));
        }
        energyConsumptionOfFineSandProductionListByDayVO.setEnergyConsumptionList(energyConsumptionLists);
        return energyConsumptionOfFineSandProductionListByDayVO;
    }

    @Override
    public ShiftProductionVO getOutputEnergyConsumptionByShift(OutputEnergyConsumptionByShiftDTO outputEnergyConsumptionByShiftDTO) {
        List<ShiftProductionEnergyVO> shiftProductionEnergy = outputEnergyConsumptionMapper.getShiftProductionEnergy(outputEnergyConsumptionByShiftDTO.getProcess(),outputEnergyConsumptionByShiftDTO.getDate(), outputEnergyConsumptionByShiftDTO.getDayShiftOrEveningShift() + "班");
        ShiftProductionVO shiftProductionVO = new ShiftProductionVO();
        log.info("shiftProductionEnergy:{}", shiftProductionEnergy);
        //设置列表
        shiftProductionVO.setShiftProductionEnergyVOList(shiftProductionEnergy);
        //总机台数
        shiftProductionVO.setOpeningTableNumber(shiftProductionEnergy.size());
        //总产量
        Float totalProduction = 0.0f;
        //总折合产量
        Float totalConvertIntoProduction = 0.0f;
        //平均效率
        Float totalEfficiency = 0.0f;
        for (ShiftProductionEnergyVO shiftProductionEnergyVO : shiftProductionEnergy) {
            totalProduction += shiftProductionEnergyVO.getProduction();
            totalConvertIntoProduction += shiftProductionEnergyVO.getConvertIntoProduction();
            totalEfficiency += shiftProductionEnergyVO.getEfficiency();
        }
        //平均效率
        totalEfficiency = totalEfficiency / shiftProductionEnergy.size();
        //设置总产量
        shiftProductionVO.setProductionAmount(totalProduction);
        //设置总折合产量
        shiftProductionVO.setEquivalentProductionAmount(totalConvertIntoProduction);
        //设置平均效率
        shiftProductionVO.setAverageEfficiency(totalEfficiency);
        return shiftProductionVO;
    }

    @Override
    public MonthlyFineSandStatsVO getOutputEnergyConsumptionByMonth(String process, String monthStr) {
        String month = monthStr.split("（")[0];
        MonthlyFineSandStatsVO monthlyFineSandStatsVO = new MonthlyFineSandStatsVO();
        //得到班组数据
        ShiftDate crewDataJia = outputEnergyConsumptionMapper.getCrewData(month,process);
        //得到班组数据
        ShiftDate crewDataYi = outputEnergyConsumptionMapper.getCrewDataYi(month,process);
        //得到班组数据
        ShiftDate crewDataBing = outputEnergyConsumptionMapper.getCrewDataBing(month,process);
        //设置班组数据
        //晚班产量
//        monthlyFineSandStatsVO.setLateYield(outputEnergyConsumptionMapper.getLateYield(monthStr,process));
        //晚班效率
//        monthlyFineSandStatsVO.setLateEfficiency(outputEnergyConsumptionMapper.getLateEfficiency(monthStr,process));
        //早班产量
//        monthlyFineSandStatsVO.setEarlyYield(outputEnergyConsumptionMapper.getEarlyYield(monthStr,process));
        //早班效率
//        monthlyFineSandStatsVO.setEarlyEfficiency(outputEnergyConsumptionMapper.getEarlyEfficiency(monthStr,process));
        //产量效率列表
//        monthlyFineSandStatsVO.setYieldAndEfficiencyList(outputEnergyConsumptionMapper.getYieldAndEfficiencyList(monthStr,process));
        return monthlyFineSandStatsVO;
    }

    @Override
    public EnergyConsumptionOfFineSandProductionListByVarietyVO getEnergyConsumptionOfFineSandProductionListByVariety(String monthStr, String process) {
        String monthStart = monthStr.substring(8, 18);
        String monthEnd = monthStr.substring(20, 31);
        EnergyConsumptionOfFineSandProductionListByVarietyVO energyConsumptionOfFineSandProductionListByVarietyVO = new EnergyConsumptionOfFineSandProductionListByVarietyVO();
        List<MonthlyVarietyDetailVO> monthlyVarietyDetail = outputEnergyConsumptionMapper.getMonthlyVarietyStats(process,monthStart,monthEnd);
        //总产量
        Float totalProduction = 0.0f;
        //总折合产量
        Float totalConvertIntoProduction = 0.0f;
        for (MonthlyVarietyDetailVO monthlyVarietyDetailVO : monthlyVarietyDetail) {
            totalProduction += monthlyVarietyDetailVO.getTotalProduction();
            totalConvertIntoProduction += monthlyVarietyDetailVO.getTotalConvertIntoProduction();
        }
        log.info("monthlyVarietyDetail:{}", monthlyVarietyDetail);
        energyConsumptionOfFineSandProductionListByVarietyVO.setTotalProduction(totalProduction);
        energyConsumptionOfFineSandProductionListByVarietyVO.setTotalConvertIntoProduction(totalConvertIntoProduction);
        energyConsumptionOfFineSandProductionListByVarietyVO.setMonthlyVarietyDetailVOList(monthlyVarietyDetail);
        return energyConsumptionOfFineSandProductionListByVarietyVO;
    }
}
